;
(function ($) {
	$.fn.slidelist = function (settings) {
		var _defaultSettings = {
			url: "",
			removeUrl: "",
			postTicket: "",
			itemCount:4
		};
		var _settings = $.extend(true, _defaultSettings, settings);
		
		function addPjHover($pj){			
			$pj.hover(function(){
				var $mask = $pj.find(".pjMask");
				var $hover = $pj.find(".pjHover");
				
				$mask.fadeIn(100);
				$hover.fadeIn(100);
			},function(){
				var $mask = $pj.find(".pjMask");
				var $hover = $pj.find(".pjHover");
				
				$mask.fadeOut(100);
				$hover.fadeOut(100);
			});
		}
		
		
		function clickPjRemove($sender){
			var id = $sender.attr("data-id");
			
			$.ajax({
				url: _settings.removeUrl,
				type: "post",
				dataType: "json",
				data: {
					project_id:id,
					post_ticket:_settings.postTicket
				},
				success: function(response){
					if(response.result == "ok"){
						if(response.data[0] == "1"){
							var $pj = $sender.parents(".pj");
							var $slide = $sender.parents(".slideBox");
							
							$pj.remove();
							if($slide.next("div.slideBox").length > 0){
								padPj($slide);
							} else{
								refreshSlide($slide);
							}
						}
					}
					else if(response.result == "no"){
						alert(response.msg);
					}
				}
			});
		}
		function padPj($slide){
			var $next = $slide.next("div.slideBox");
			if($next.length > 0){
				$next.find(".pj").first().appendTo($slide);
				if($next.find(".pj").length > 0)
					padPj($next);
				else
					$next.remove();
			}
		}
		function refreshSlide($slide){
			var pg = $slide.parents(".slideContainer").data("pg");
			
			$.ajax({
				url: _settings.url,
				type: "post",
				dataType: "json",
				data: {
					pg:pg,
					itemCount:_settings.itemCount
				},
				success: function(response){
					var html = "";
					if(response.result == "ok"){
						html = response.data[0];
					}
					else if(response.result == "no"){
					}
						
					if(html != ""){
						$slide.html(html);

						$slide.find(".pj").each(function(){
							addPjHover($(this));
							$(this).find(".pjRemove").click(function(){
								clickPjRemove($(this));
							});
						});
							
						$slide.find("img.imgPjBox").imgresize({
							width:205,
							height:205
						});
					}
				}
			});
		}
		
		$(this).each(function () {
			var $pB = $(this).find(".preBtn").addClass("disabled");
			var $nB = $(this).find(".nextBtn");
			var $container = $(this).find(".slideContainer");
			var $longBox = $(this).find(".slideLongBox");
			var $slideBox = $(this).find(".slideBox");
			
			$slideBox.width($container.width()).addClass("pg1");
			$container.data("pg",1);
			
			$slideBox.find(".pj").each(function(){
				addPjHover($(this));
				$(this).find(".pjRemove").click(function(){
					clickPjRemove($(this));
				});
			});
			
			$pB.click(function(){
				var pg = $container.data("pg")-1;
				
				if($(this).hasClass("disabled"))return ;
				if(pg < 1){
					$pB.addClass("disabled");
					return ;
				}
				
				var width = $container.width();
				$longBox.animate({
					"left":(0-((pg-1) * width)) + "px"
				},function(){
					$pB.removeClass("disabled");
					$nB.removeClass("disabled");
					$container.data("pg",pg);
				});
				
			});
			$nB.click(function(){
				var pg = $container.data("pg")+1;
				
				if($(this).hasClass("disabled"))return ;
				if(pg < 1)	return ;
				if(_settings.url == "")	return;
				
				$pB.addClass("disabled");
				$nB.addClass("disabled");
				
				if($longBox.find(".pg" + pg).length > 0){
					var width = $container.width();
					$longBox.animate({
						"left":(0-((pg-1) * width)) + "px"
					},function(){
						$pB.removeClass("disabled");
						$nB.removeClass("disabled");
						$container.data("pg",pg);
					});
					return;
				}
				$.ajax({
					url: _settings.url,
					type: "post",
					dataType: "json",
					data: {
						pg:pg,
						itemCount:_settings.itemCount
					},
					success: function(response){
						var html = "";
						if(response.result == "ok"){
							html = response.data[0];
						}
						else if(response.result == "no"){
						}
						
						if(html != ""){
							var width = $container.width();
							var $div = $("<div></div>").css("float","left").width(width).addClass("slideBox").addClass("pg" + pg).html(html);
							$longBox.width($longBox.width() + width).append($div);
							$div.find(".pj").each(function(){
								addPjHover($(this));
								$(this).find(".pjRemove").click(function(){
									clickPjRemove($(this));
								});
							});
							
							$div.find("img.imgPjBox").imgresize({
								width:260,
								height:200
							});
							
							$longBox.animate({
								"left":(0-((pg-1) * width)) + "px"
							},function(){
								$pB.removeClass("disabled");
								$nB.removeClass("disabled");
								$container.data("pg",pg);
							});
						} else{
							$pB.removeClass("disabled");
						}
						
					}
				});
			});
			
			
		});
	};
})(jQuery);